package com.youko.customerfrontstage.web;

import com.youko.customerfrontstage.bean.Commodity;
import com.youko.customerfrontstage.bean.CommoditySpu;
import com.youko.customerfrontstage.bean.PageQuery;
import com.youko.customerfrontstage.bean.PicturePath;
import com.youko.customerfrontstage.dto.commodity.MainPageSpuDto;
import com.youko.customerfrontstage.dto.commodity.SpuReturnDto;
import com.youko.customerfrontstage.mapper.CommoditySpuMapper;
import com.youko.customerfrontstage.service.CommodityService;
import com.youko.customerfrontstage.service.Lucene.ILuceneService;
import com.youko.customerfrontstage.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: youko
 * @classname: CommoditySpuSearchController
 * @Description: some desc
 * @date: 2022/7/1 15:55
 */
@RestController
public class CommoditySpuSearchController {
    @Autowired
    private ILuceneService service;
    @Autowired
    private CommoditySpuMapper commoditySpuMapper;

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private PictureService pictureService;





    @PostMapping("/search")
    public PageQuery<CommoditySpu> searchCommoditySpu(@RequestBody PageQuery<CommoditySpu> pageQuery) throws IOException, org.apache.lucene.queryparser.classic.ParseException {
        PageQuery<CommoditySpu> pageQuery1=service.searchCommoditySpu(pageQuery);
        return pageQuery1;
    }

    /**
     * 测试查询所有商品
     * @return
     */
    @GetMapping("/selectAllSpu")
    public List<CommoditySpu> findAll(){
        return commoditySpuMapper.getAllCommoditySpu();
    }

    /**
     * 按照三级分类查询商品
     */
    @GetMapping("/selectSpuByCategory")
    public List<MainPageSpuDto> selectSpuByCategory(int categoryId){
        List<CommoditySpu> commoditySpuList=commoditySpuMapper.selectAllByCategory(categoryId);
        List<MainPageSpuDto> pList = new ArrayList<MainPageSpuDto>();
        for (CommoditySpu cs:commoditySpuList){
            MainPageSpuDto mainPageSpuDto=new MainPageSpuDto();
            mainPageSpuDto.setSpu(cs);
            mainPageSpuDto.setPicPath(pictureService.getMainPagePic(cs.getId()).getPicPath());
            pList.add(mainPageSpuDto);
        }
        return pList;
    }

    /**
     *根据id查询Spu
     */
    @GetMapping("/selectSpuById")
    public SpuReturnDto selectSpuById(int spuId){
        SpuReturnDto spuReturnDto=new SpuReturnDto();
        CommoditySpu spu = commoditySpuMapper.getCommoditySpuById(spuId);
        spuReturnDto.setSpu(spu);

        List<PicturePath> pictures = pictureService.getPicture(spuId);
        List<String> picPath=new ArrayList<String>(5);
        for (int i=0;i<pictures.size();i++){
            picPath.add(pictures.get(i).getPicPath());

        }
        spuReturnDto.setPicPath(picPath);

        return spuReturnDto;
    }

}
